#!/bin/sh

# Licensed to the .NET Foundation under one or more agreements.
# The .NET Foundation licenses this file to you under the MIT license.
# See the LICENSE file in the project root for more information.
#
#     mdtool_gcc
#
#Abstract:
#  Generates dependencies for a makefile
#  (for gcc compilers)
#

str=`grep -n '#mdtool output goes here>' obj${BUILD_ALT_DIR}/${_BUILDARCH}/makefile`
if [ "$str" = "" ] ; then
#didn't find our placeholder string : don't change file
    echo mdtool_gcc : makefile not setup for mdtool : ignoring
    exit 0
fi

#truncate makefile to remove old dependencies
sed /'#mdtool output goes here>'/q obj${BUILD_ALT_DIR}/${_BUILDARCH}/makefile > obj${BUILD_ALT_DIR}/${_BUILDARCH}/makefile.final

echo '#dependencies generated by mdtool_gcc :' >> obj${BUILD_ALT_DIR}/${_BUILDARCH}/makefile.final
echo >> obj${BUILD_ALT_DIR}/${_BUILDARCH}/makefile.final
# gcc -M generates make-friendly dependencies;
# -MM ignores system includes (#include <file.h>)
@CC@ @MDTOOL_CFLAGS@ $@ >> obj${BUILD_ALT_DIR}/${_BUILDARCH}/makefile.depfromgcc
#Prepend ../.. to the source's filename
#with some sed magic, since gcc doesn't do it for us.
sed 's#: \(.*\)$#: ../../\1#g' obj${BUILD_ALT_DIR}/${_BUILDARCH}/makefile.depfromgcc > obj${BUILD_ALT_DIR}/${_BUILDARCH}/makefile.aftersed
cat obj${BUILD_ALT_DIR}/${_BUILDARCH}/makefile.aftersed >> obj${BUILD_ALT_DIR}/${_BUILDARCH}/makefile.final

if [ "$?" = "0" ] ; then
   echo mdtool_gcc : dependencies generated
   # replace old makefile by new one.
   mv -f obj${BUILD_ALT_DIR}/${_BUILDARCH}/makefile.final obj${BUILD_ALT_DIR}/${_BUILDARCH}/makefile
   rm -f obj${BUILD_ALT_DIR}/${_BUILDARCH}/makefile.aftersed
   rm -f obj${BUILD_ALT_DIR}/${_BUILDARCH}/makefile.depfromgcc
   exit 0
fi
exit 1

